package com.bytedance.common.utility;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;
import android.widget.Toast;

import com.ss.android.article.browser.R;

public final class UIUtils {
    public static final class a {
        public String a;
        public int b;

        public a() {

        }
    }

    public static a a = new a();
    private static String b = "";
    private static int densityDpi = -1;

    public static void showImageToast(Context context, int arg2, int arg3) {
        UIUtils.showImageToast(context, arg2, context.getString(arg3));
    }

    public static float sp2px(Context context, float sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
    }

    public static int a(float arg1) {
        return ((int) (0.999f + arg1));
    }

    public static final String a(int arg7) {
        String v0;
        if (arg7 > 10000) {
            v0 = String.format("%.1f", Double.valueOf(1 * (((double) arg7)) / 10000));
            v0 = 48 == v0.charAt(v0.length() - 1) ? v0.substring(0, v0.length() - 2) + "万" : v0 + "万";
        } else {
            v0 = String.valueOf(arg7);
        }

        return v0;
    }

    public static void a() {
        int v0 = Looper.myLooper() == Looper.getMainLooper() ? 1 : 0;
        if (v0 == 0) {
            Logger.throwException("not in UI thread");
        }
    }

    public static void requestActivityOrientation(Activity activity, boolean landscape) {
        if (activity != null && !activity.isFinishing()) {
            int orientation = landscape ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            activity.setRequestedOrientation(orientation);
            if (landscape) {
                activity.getWindow().setFlags(1024, 1024);
            } else {
                activity.getWindow().clearFlags(1024);
            }
        }
    }

    public static void a(Context arg2) {
        UIUtils.showImageToast(arg2, 0, arg2.getString(com.ss.android.article.browser.R.string.ss_error_no_connections));
    }

    public static void showImageToast(Context context, int arg3, String arg4) {
        UIUtils.showToast(context, arg3, arg4, 0, 17);
    }

    static void a(Context arg0, int arg1, String arg2, int arg3, int arg4) {
        UIUtils.showToast(arg0, arg1, arg2, arg3, arg4);
    }

    public static void a(Context arg1, String arg2) {
        UIUtils.showImageToast(arg1, 0, arg2);
    }

    public static void setTopMargin(View view, float dp) {
        if (view != null) {
            UIUtils.setLayoutMargins(view, -3,
                    ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, view.getContext().getResources().getDisplayMetrics())),
                    -3, -3);
        }
    }

    public static void setLayoutMargins(View view, int leftMargin, int topMargin, int rightMargin, int bottomMargin) {
        if (view != null) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null && ((lp instanceof ViewGroup.MarginLayoutParams)) && view != null && lp != null) {
                if (((ViewGroup.MarginLayoutParams) lp).leftMargin == leftMargin
                        && ((ViewGroup.MarginLayoutParams) lp).topMargin == topMargin
                        && ((ViewGroup.MarginLayoutParams) lp).rightMargin == rightMargin
                        && ((ViewGroup.MarginLayoutParams) lp).bottomMargin == bottomMargin) {
                    return;
                }

                if (leftMargin != -3) {
                    ((ViewGroup.MarginLayoutParams) lp).leftMargin = leftMargin;
                }

                if (topMargin != -3) {
                    ((ViewGroup.MarginLayoutParams) lp).topMargin = topMargin;
                }

                if (rightMargin != -3) {
                    ((ViewGroup.MarginLayoutParams) lp).rightMargin = rightMargin;
                }

                if (bottomMargin != -3) {
                    ((ViewGroup.MarginLayoutParams) lp).bottomMargin = bottomMargin;
                }

                view.setLayoutParams(lp);
            }
        }
    }

    public static void setBackgroundResource(View view, int resId) {
        if (view != null) {
            int paddingLeft = view.getPaddingLeft();
            int paddingRight = view.getPaddingRight();
            int paddingTop = view.getPaddingTop();
            int paddingBottom = view.getPaddingBottom();
            view.setBackgroundResource(resId);
            view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        }
    }

    public static void setLayoutParams(View view, int width, int height) {
        if (view != null) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null) {
                if (lp.width == width && lp.height == height) {
                    return;
                }

                if (width != -3) {
                    lp.width = width;
                }

                if (height != -3) {
                    lp.height = height;
                }

                view.setLayoutParams(lp);
            }
        }
    }

    public static void setBackgroundColor(View view, Resources resources, int colorId) {
        if (view != null) {
            int paddingLeft = view.getPaddingLeft();
            int paddingRight = view.getPaddingRight();
            int paddingTop = view.getPaddingTop();
            int paddingBottom = view.getPaddingBottom();
            view.setBackgroundColor(resources.getColor(colorId));
            view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        }
    }

    public static void setBackgroundDrawable(View view, Drawable drawable) {
        if (view != null) {
            int paddingLeft = view.getPaddingLeft();
            int paddingRight = view.getPaddingRight();
            int paddingTop = view.getPaddingTop();
            int paddingBottom = view.getPaddingBottom();
            view.setBackgroundDrawable(drawable);
            view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        }
    }

    public static void getDistance(View view, View view2, int[] location) {
        if (view != null && view2 != null) {
            view.getLocationInWindow(location);
            int x1 = location[0];
            int y1 = location[1];
            view2.getLocationInWindow(location);
            int distanceX = location[0] - x1;
            int distanceY = location[1] - y1;
            location[0] = distanceX;
            location[1] = distanceY;
        }
    }

    @SuppressLint(value = {"NewApi"})
    public static void setMaxLines(TextView tv, int maxLines) {
        if (tv != null
                && maxLines > 0
                && (Build.VERSION.SDK_INT < 16 || tv.getMaxLines() != maxLines)) {
            tv.setSingleLine(maxLines == 1);
            tv.setMaxLines(maxLines);
        }
    }

    public static void setTextAndVisibility(TextView textView, CharSequence text) {
        if (textView != null) {
            if (TextUtils.isEmpty(text)) {
                UIUtils.setVisibility(textView, 8);
            } else {
                UIUtils.setVisibility(textView, 0);
                textView.setText(text);
            }
        }
    }

    public static void a(String arg8, int arg9, Paint arg10, int arg11, a arg12) {
        if (arg9 <= arg11 || (android.support.design.a.isTextEmpty(arg8))) {
            arg12.a = "";
            arg12.b = 0;
        } else {
            int v0 = ((int) (arg10.measureText(arg8) + 0.999f));
            if (v0 <= arg9) {
                arg12.a = arg8;
                arg12.b = v0;
            } else {
                StringBuilder v7 = new StringBuilder();
                v0 = arg10.breakText(arg8, 0, arg8.length(), true, ((float) (arg9 - arg11)), null);
                if (v0 <= 0) {
                    arg12.a = "";
                    arg12.b = 0;
                } else {
                    v7.append(arg8.substring(0, v0));
                    v7.append('…');
                    arg12.a = v7.toString();
                    arg12.b = arg9;
                }
            }
        }
    }

    public static void setOnClickListener(boolean clickable, View view, View.OnClickListener onClickListener) {
        if (view != null) {
            if (clickable) {
                view.setOnClickListener(onClickListener);
                view.setClickable(true);
            } else {
                view.setOnClickListener(null);
                view.setClickable(false);
            }
        }
    }

    public static final boolean isVisible(View view) {
        return view != null && view.getVisibility() == View.VISIBLE;
    }

    public static int[] a(View child, View ancestorView) {
        int[] v0_2;
        int[] v2 = null;
        int v0 = 2;
        float v8 = 0.5f;
        if (child == null || ancestorView == null) {
            Logger.throwException("invalid params: child:" + child + ",ancestor:" + ancestorView);
            v0_2 = v2;
        } else {
            int[] v3 = new int[v0];
            float[] v6 = new float[v0];
            v6[1] = 0f;
            v6[0] = 0f;
            v6[0] += ((float) child.getLeft());
            v6[1] += ((float) child.getTop());
            ViewParent v1 = child.getParent();
            while (true) {
                if ((v1 instanceof View)) {
                    ViewParent v0_1 = v1;
                    if (v1 == ancestorView) {
                        v0 = 1;
                    } else {
                        v6[0] -= ((float) ((View) v0_1).getScrollX());
                        v6[1] -= ((float) ((View) v0_1).getScrollY());
                        v6[0] += ((float) ((View) v0_1).getLeft());
                        v6[1] += ((float) ((View) v0_1).getTop());
                        v1 = ((View) v0_1).getParent();
                        continue;
                    }
                } else {
                    v0 = 0;

                }
                break;
            }
            if (v0 == 0) {
                Logger.throwException("ancestorView:" + ancestorView + " is not the ancestor of child : " + child);
                v0_2 = v2;
                return v0_2;
            }

            v3[0] = ((int) (v6[0] + v8));
            v3[1] = ((int) (v6[1] + v8));
            v0_2 = v3;
        }

        return v0_2;
    }

    private static void showToast(Context context, int arg8, String text, int duration, int gravity) {
        int v0 = 0;
        if (context != null && !android.support.design.a.isTextEmpty(text)) {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                v0 = 1;
            }

            if (v0 == 0) {
                new Handler(Looper.getMainLooper()).post(new h(context, arg8, text, duration, gravity));
                return;
            }

            if ((context instanceof ICustomToast)) {
                if (duration == 1) {
                    ((ICustomToast) context).showCustomLongToast(arg8, text);
                    return;
                }

                if (duration == 0) {
                    duration = 2000;
                }
                ((ICustomToast) context).showCustomToast(arg8, text, duration, gravity);
                return;
            }

            try {
                Toast toast = Toast.makeText(context, text, duration);
                if (toast == null) {
                    return;
                }
                toast.setGravity(gravity, 0, 0);
                toast.show();
            } catch (Exception exception) {
                Logger.throwExceptionInDebug(exception);
            }
        }
    }

    public static final void setVisibility(View view, int targetVisibility) {
        if (view != null && view.getVisibility() != targetVisibility) {
            boolean isValidVisibility = targetVisibility == View.VISIBLE
                    || targetVisibility == View.GONE
                    || targetVisibility == View.INVISIBLE;
            if (!isValidVisibility) {
                return;
            }
            view.setVisibility(targetVisibility);
        }
    }

    public static float dp2px(Context context, float dp) {
        return context.getResources().getDisplayMetrics().density * dp + 0.5f;
    }

    public static int b(int arg2) {
        return 16777215 & arg2 | -1308622848;
    }

    public static final int getScreenWidth(Context context) {
        if (context != null) {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            if (metrics != null) {
                return metrics.widthPixels;
            }
        }
        return 0;
    }

    public static int b(View arg3) {
        int v0_1;
        int v1 = -1;
        if (arg3 == null || arg3.getParent() == null) {
            v0_1 = v1;
        } else {
            ViewParent v0 = arg3.getParent();
            v0_1 = (v0 instanceof ViewGroup) ? ((ViewGroup) v0).indexOfChild(arg3) : v1;
        }

        return v0_1;
    }

    public static void showImageToast(Context context, String content) {
        UIUtils.showImageToast(context, R.drawable.close_popup_textpage, content);
    }

    public static void setText(TextView textView, CharSequence txt) {
        if (textView != null || !TextUtils.isEmpty(txt)) {
            textView.setText(txt);
        }
    }

    public static final int getScreenHeight(Context context) {
        if (context != null) {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            if (metrics != null) {
                return metrics.heightPixels;
            }
        }
        return 0;
    }

    public static int px2dp(Context context, float px) {
        return (int) (px / context.getResources().getDisplayMetrics().density + 0.5f);
    }

    public static boolean c(View view) {
        if (view == null || view.getAnimation() == null) {
            return false;
        } else {
            view.clearAnimation();
            return true;
        }
    }

    public static final int percentWidth(Context context, float percent) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics == null ? 0 : ((int) ((float) displayMetrics.widthPixels * percent));
    }

    public static String d(Context arg3) {
        if ((android.support.design.a.isTextEmpty(UIUtils.b)) && arg3 != null) {
            int v0 = UIUtils.getScreenWidth(arg3);
            int v1 = UIUtils.getScreenHeight(arg3);
            if (v0 > 0 && v1 > 0) {
                UIUtils.b = v0 + "*" + v1;
            }
        }

        return UIUtils.b;
    }

    public static int getDensityDpi(Context arg2) {
        if (UIUtils.densityDpi == -1 && arg2 != null) {
            UIUtils.densityDpi = arg2.getApplicationContext().getResources().getDisplayMetrics().densityDpi;
        }

        return UIUtils.densityDpi;
    }

    public static int diggLayoutWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels * 1375 / 10000 + (int) UIUtils.dp2px(context, 20f);
    }

    public static final int getStatusBarHeight(Context context) {
        int v0 = 0;
        if (context != null) {
            int v1 = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (v1 > 0) {
                v0 = context.getResources().getDimensionPixelSize(v1);
            }
        }

        return v0;
    }
}

